<?php
namespace app\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class PriceSettingMaster extends Model
{

    // 设置表名NAME="pricesetting_master";
    // 设置表名
    const TABLE_NAME = "price_setting_master";

    protected $table = 'price_setting_master';

    protected $primaryKey = 'price_setting_id';

    protected $PriceSettingMaster = self::TABLE_NAME;

    public $timestamps = false;

    public function getPriceSettingInfo()
    {
        $sql = 'SELECT t.category_id,t.category_name FROM category_master t WHERE delete_flg=0 order by t.category_id ';
        return DB::select($sql);
    }

    public function getPriceGroupId()
    {
        $sql = 'SELECT DISTINCT t.group_id FROM price_setting_master t  where t.delete_flg = 0 ORDER BY t.group_id  ASC ';
        return DB::select($sql);
    }

    public function getPriceGroupInfo($GroupId)
    {
        $sql = 'SELECT t.category_id, t.category_name, b.price_setting_id, b.percent FROM category_master t LEFT JOIN ( SELECT u.price_setting_id, u.category_id, u.percent FROM price_setting_master u WHERE u.group_id=? AND u.delete_flg = 0 ) AS b on t.category_id=b.category_id WHERE delete_flg = 0 ORDER BY t.category_id ';
        $param = [
            $GroupId
        ];
        return DB::select($sql, $param);
    }

    public function udpPriceGroupInfo($groupId, $catIds, $priceseIfos)
    {
        for ($i = 0; $i < count($catIds); $i ++) {

            $param = [
                $catIds[$i],
                $groupId
            ];

            $sql = 'SELECT 1 FROM price_setting_master WHERE category_id = ? AND group_id = ? AND delete_flg = 0   ';

            $priceIfo = DB::select($sql, $param);

            if (empty($priceIfo)) {
                $param = [
                    $catIds[$i],
                    $groupId,
                    $priceseIfos[$catIds[$i]] / 100
                ];
                $sql = 'insert into price_setting_master VALUES(null,?,?,?,0,now(),now())';

                DB::insert($sql, $param);
            } else {
                $param = [
                    $priceseIfos[$catIds[$i]] / 100,
                    $catIds[$i],
                    $groupId
                ];

                $sql = 'update price_setting_master t set t.percent=? , t.updated_at=now() where t.category_id=? and t.group_id=?';

                DB::update($sql, $param);
            }
        }
        return true;
    }

    public function delPriceGroupInfo($groupId)
    {
        $sql = 'UPDATE price_setting_master SET delete_flg = 1 WHERE  group_id = ? ';
        $param = [
            $groupId
        ];

        return DB::update($sql, $param);
    }

    public function getPriceByGroupId($groupId)
    {
        $sql = 'SELECT 1 FROM price_setting_master t WHERE  t.group_id = ? AND t.delete_flg = 0';

        $param = [
            $groupId
        ];

        return DB::select($sql, $param);
    }

    public function addPriceGroupInfo($groupId, $catIds, $priceseIfos)
    {
        DB::beginTransaction();

        try {
            for ($i = 0; $i < count($catIds); $i ++) {

                $param = [
                    $catIds[$i],
                    $groupId,
                    $priceseIfos[$catIds[$i]] / 100
                ];

                $sql = 'insert into price_setting_master VALUES(null,?,?,?,0,now(),now())';

                DB::insert($sql, $param);
            }

            DB::commit();
        } catch (\Exception $e) {
            var_dump($e);

            DB::rollBack();
            return false;
        }

        return true;
    }
}
?>

